<?php    
    namespace app\user;
    include_once('../autoloader.php');
    include_once('../functions.php');

    use app\repository\UserRepository;
    use app\HeadComponent;
    use app\NavComponent;

    // 启用session，解决全局登录的问题
    session_start();

    if (!UserRepository::isLogin()) {
        header('Location: ./../login.view.php');
    }

    $page = 0;
    $username = '';
    $name = '';

    if (isset($_GET['page'])) {
        $page = (int) $_GET['page'];
    }      

    if (isset($_GET['username'])) {
        $username = $_GET['username'];
    }     

    if (isset($_GET['name'])) {
        $name = $_GET['name'];
    }
   
    echo HeadComponent::render('用户管理');
?>
<body class="container">

    <div class="row">
        <div class="col-12">
            <? echo NavComponent::render(); ?>
        </div>
    </div>

    <div class="row">
        <div class="col-12">
            <form>
                <div class="row mt-2">
                    <div class="col">
                        <input type="hidden" name="page" value="<? echo $page; ?>">
                        <input type="text" class="form-control" placeholder="用户名" name="username" value="<? echo $username; ?>">
                    </div>
                    <div class="col">
                        <input type="text" class="form-control" placeholder="姓名" name="name"
                        value="<? echo $name;?>">
                    </div>
                    <div class="col">
                        <button class="btn btn-info">查询</button>
                    </div>
                </div>
            </form>
        </div>
        <hr>
        <div class="col-12 text-right">
            <a href="add.view.php" class="btn btn-sm btn-primary"><i class=""></i>新增</a>
        </div>
        <hr>
        <div class="col-12">

            <table class="table table-striped">
                <thead>
                    <tr>
                        <th scope="col">序号</th>
                        <th scope="col">姓名</th>
                        <th scope="col">用户名</th>
                        <th scope="col">角色</th>
                        <th scope="col">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        $userPage = UserRepository::page($username, $name, $page);

                        $users = $userPage->content;

                        foreach($users as $key => $user) {
                            echo '
                            <tr>
                                <th scope="row">' . ($userPage->number * $userPage->size + $key + 1) .'</th>
                                <td>' . $user['name'] . '</td>
                                <td>' . $user['username']. '</td>
                                <td>' . ($user['role'] === '0' ? '管理员' : '用户') . '</td>
                                <td>
                                    <a href="edit.view.php?id=' . $user['id'] . '" class="btn btn-outline-primary btn-sm">修改</a>
                                    <a href="delete.controller.php?id=' . $user['id'] . '" class="btn btn-outline-danger btn-sm">删除</a>
                                </td>
                            </tr>
                            ';
                        }
                    ?>
                    
                </tbody>
            </table>
            <div class="row">
                <div class="offset-4 col-4">
                    <? include('pagination.php'); ?>
                </div>
            </div>
        </div>
    </div>
</body>
